﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AntdUI;
using NetTaste;
namespace TestPressure
{
    public partial class DeviceManageForm : BaseForm
    {
        #region

        private Device? obj;

        #endregion

        #region 自定义方法

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<Device> list = SqlSugarHelper.Inquireable<Device>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<Device> list)
        {
            var antItemList = new List<DeviceItem>();
            foreach (Device item in list)
            {
                antItemList.Add(new DeviceItem(item.device_number, item.device_name, item.device_address, item.port_address,
                                               item.baudrate));
            }
            Table1.DataSource = antItemList;
        }

        /// <summary>根据编号查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Device? DeviceWithNumber(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["device_number"] = number;
            List<Device> list = SqlSugarHelper.Inquireable<Device>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckSelectedRowExist(int number)
        {
            obj = DeviceWithNumber(number);
            if (obj == null)
            {
                LoadList();
                string msg = String.Format("【{0}】编号设备不存在", number);
                Error(msg);
                return false;
            }
            else
            {
                return true;
            }
        }

        #endregion

        #region 系统方法
        public DeviceManageForm()
        {
            InitializeComponent();
            this.EnableMove = false;
            //this.FullScreen = true;
        }

        private void DeviceManageForm_Load(object sender, EventArgs e)
        {
            this.StackPanel1.Top = this.Header.Bottom;
            this.StackPanel1.Left = this.Header.Left;
            this.StackPanel1.Width = this.Width;
            this.Table1.Top = this.StackPanel1.Bottom;
            this.Table1.Left = this.Header.Left;
            this.Table1.Width = this.Width;
            this.Table1.Height = this.Height - this.StackPanel1.Height - 10;
            this.Header.Text = "设备管理";
            this.WindowState = FormWindowState.Maximized;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("deviceNumber","编号"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("deviceName","名称"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("deviceAddress","地址"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("portAddress","串口"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("baudRate","波特率"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("operation","操作"){ Fixed=true,Width="auto",ColAlign=AntdUI.ColumnAlign.Center}
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table1.Spin(config, () =>
            {
                LoadList();
            }, () =>
            {

            });
        }
        /// <summary>添加设备
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddDeviceButton_Click(object sender, EventArgs e)
        {
            AddDeviceForm frm = new AddDeviceForm();
            frm.Owner = this;
            frm.ShowDialog();
        }
        
        /// <summary>点击操作按钮
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Table1_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            if (e.Record is DeviceItem data)
            {
                AntdUI.CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                    bool res = CheckSelectedRowExist(data.deviceNumber);
                    if (!res)
                    {
                        return;
                    }
                    if (obj is not null)
                    {
                        EditDeviceForm frm = new EditDeviceForm();
                        frm.obj = obj;
                        frm.Owner = this;
                        frm.ShowDialog();
                    }
                    else
                    {
                        Error("编辑设备失败");
                    }
                }
                else if (link.Id == "delete")
                {
                    bool res = CheckSelectedRowExist(data.deviceNumber);
                    if (!res)
                    {
                        return;
                    }
                    string msg = string.Format("您确定要删除【{0}】设备吗？",data.deviceName);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            if (obj is not null)
                            {
                                List<Device> list = new List<Device>();
                                list.Add(obj);
                                int deleteRes = SqlSugarHelper.Deleteable(list);
                                if (deleteRes > 0)
                                {
                                    LoadList();
                                }
                                else
                                {
                                    Error("删除设备失败");
                                    
                                }
                            }
                            else
                            {
                                Error("删除设备失败");
                            }
                            return true;
                        },
                    });
                }
                else if (link.Id == "point")
                {
                    bool res = CheckSelectedRowExist(data.deviceNumber);
                    if (!res)
                    {
                        return;
                    }
                    DevicePointManageForm frm = new DevicePointManageForm();
                    frm.Owner = this;
                    frm.item = data;
                    frm.ShowDialog();
                }
            }
        }

        #endregion
    }
}
